<?php

namespace App\Utils\Filter\Admin\System;

use App\Entity\AdminUser;
use App\Utils\Base\FilterBase;
use App\Utils\Helper\GetterHelper;

class LogFilter extends FilterBase {
    public function logTypeId ($field, $value) {
        $this->builder->andwhere($field.' = :log_type_id')->setParameter('log_type_id', $value);
    }

    public function adminUser ($field, $value) {
        $ids = [];
        $admin_user_ids = GetterHelper::getEntityManager()->getRepository(AdminUser::class)->getAdminUserIdsByUsername($value);
        foreach ($admin_user_ids as $item) {
            array_push($ids, $item['id']);
        }
        $this->builder->andwhere(FilterBase::ALIAS.'.admin_user_id in (:admin_user_ids)')->setParameter('admin_user_ids', $ids);
    }

    public function date ($field, $value) {
        if (is_array($value) && count($value) == 2) {
            $start = $value[0].' 00:00:00';
            $end = $value[1].' 23:59:59';
            $this->builder->andwhere($this->builder->expr()->between(FilterBase::ALIAS.'.created_at', "'".$start."'", "'".$end."'"));
        }
    }
}